package com.yupi.shanoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.shanoj.mapper.CommentsMapper;
import com.yupi.shanoj.mapper.UserMapper;
import com.yupi.shanoj.model.dto.comment.CommentTree;
import com.yupi.shanoj.model.entity.Comments;
import com.yupi.shanoj.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 23949
* @description 针对表【comments(评论表)】的数据库操作Service实现
* @createDate 2024-07-15 20:52:36
*/
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
    implements CommentsService{

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean sendComment(Comments comments) {
        int insert = commentsMapper.insert(comments);
        return insert > 0;
    }

    @Override
    public List<CommentTree> getCommentTreeByitemid(Integer itemId,Long offset,Integer type) {
        //查询父级评论
        List<Comments> rootComments=getRootCommentsById(itemId,offset,type);

        //并行执行每个根节点评论的字评论查询任务
        List<CommentTree> commentTreeList=rootComments.stream().parallel().map(
                rootComment->buildCommentTree(rootComment,0L,0L)
        ).collect(Collectors.toList());

        return commentTreeList;
    }

    @Override
    public List<Comments> getRootCommentsById(Integer itemId,Long offset,Integer type){
        QueryWrapper<Comments> wrapper=new QueryWrapper<>();
        wrapper.eq("parentId",0).ne("isDelete",1);
        if(type==1){
            wrapper.orderByDesc("(likes-dislikes)").last("Limit 10 offset "+offset);
        }else{
            wrapper.orderByDesc("createTime").last("Limit 10 offset "+offset);
        }

        return commentsMapper.selectList(wrapper);
    }

    @Override
    public Long likeOrDislike(Integer mid, Integer uid) {
        return null;
    }

    private CommentTree buildCommentTree(Comments rootComment,Long start,Long stop){
        CommentTree commentTree=new CommentTree();
        commentTree.setId(rootComment.getId());
        commentTree.setItemId(rootComment.getItemId());
        commentTree.setUserVO(userMapper.selectById(rootComment.getUserId()));
        commentTree.setToUserVO(userMapper.selectById(rootComment.getToUserId()));
        commentTree.setContent(rootComment.getContent());
        commentTree.setCreateTime(rootComment.getCreateTime());
        commentTree.setParentId(rootComment.getParentId());
        commentTree.setLikes(rootComment.getLikes());
        commentTree.setDislikes(rootComment.getDislikes());
        if(rootComment.getParentId()==0){
            List<Comments> childComments=getChildCommentsById(rootComment.getId(),start,stop);

            List<CommentTree> childTreeList=childComments.stream().parallel().map(
                    childComment->buildCommentTree(childComment,start,stop)
            ).collect(Collectors.toList());
            commentTree.setReplies(childTreeList);
        }

        return commentTree;

    }

    private List<Comments> getChildCommentsById(Long parentId,Long start,Long stop){
        QueryWrapper<Comments> wrapper=new QueryWrapper<>();
        wrapper.eq("parentId",parentId).ne("isDelete",1);
        return commentsMapper.selectList(wrapper);
    }
}




